/**
 * 2010-4-12 
 */

import java.io.*;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * ZIP压缩工具 
 *
 * @author 梁栋
 * @since 1.0
 */
public class ZipUtils {

    public static final String EXT = ".zip";
    private static final String BASE_DIR = "";
    private static final String PATH = File.separator;
    private static final int BUFFER = 1024;

    public static void main(String[] args)  {
            File in=new File("J:\\test\\111\\1");
            File out=new File("J:\\test\\222");

        CheckedInputStream cis = null;
        try {
            cis = new CheckedInputStream(new FileInputStream(
                    in), new CRC32());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        ZipInputStream zis = new ZipInputStream(cis);

        try {
            decompress(out, zis);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }




    /**
     * 文件 解压缩 
     *
     * @param destFile
     *            目标文件 
     * @param zis
     *            ZipInputStream 
     * @throws Exception
     */
    private static void decompress(File destFile, ZipInputStream zis) throws Exception {

        ZipEntry entry = null;
        while ((entry = zis.getNextEntry()) != null) {

            // 文件  
            String dir = destFile.getPath() + File.separator + entry.getName();

            File dirFile = new File(dir);

            // 文件检查  
            fileProber(dirFile);

            if (entry.isDirectory()) {
                dirFile.mkdirs();
            } else {
                decompressFile(dirFile, zis);
            }

            zis.closeEntry();
        }
    }

    /**
     * 文件探针 
     *
     *
     * 当父目录不存在时，创建目录！ 
     *
     *
     * @param dirFile
     */
    private static void fileProber(File dirFile) {

        File parentFile = dirFile.getParentFile();
        if (!parentFile.exists()) {

            // 递归寻找上级目录  
            fileProber(parentFile);

            parentFile.mkdir();
        }

    }

    /**
     * 文件解压缩 
     *
     * @param destFile
     *            目标文件 
     * @param zis
     *            ZipInputStream 
     * @throws Exception
     */
    private static void decompressFile(File destFile, ZipInputStream zis) throws IOException {

        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destFile));

        int count;
        byte data[] = new byte[BUFFER];
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            bos.write(data, 0, count);
        }

        bos.close();
    }

}  